Introduction to JavaScript (Volume 1) (Web Programming for Beginners Series) by Randall Robertson

Introduction to JavaScript (Volume 1) (Web Programming for Beginners Series) by Randall Robertson

Author:Randall Robertson [Robertson, Randall]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-06T16:00:00+00:00


Chapter 11

JavaScript Conditional Statements: Part 2

11.1 The if…else statement The if statement executes a statement if a specified condition is true. If you want to execute some code if a condition is true and another code if the condition is not true, use the if....else statement. Use the else statement to indicate the block of code that should be executed if the condition is false.

if ( condition) {

block of code to be executed if the condition is true

} else {

block of code to be executed if the condition is false

}

Consider the following example: If the age of the user is less than 21, create a message that says: "You're too young to drink." and assign it to a variable called message. If the condition is false, create a message that says: "You're old enough to drink." and assign it to a variable called message.

if (age < 21) {

message = "You're too young to drink."; document.write (message);

} else {

message = "Your old enough to drink"; document.write (message);

}

The result dislpayed in the browser will be:

if condition is true: You're too young to drink. if condition is false: You're old enough to drink.

Syntax for if…else statement:

if (condition)

statement1

else

statement2

condition

An expression, usually an arithmetic or Boolean comparison, that evaluates to either true or false. statement1

statement1 is a block of code that is executed if condition evaluates to true. The statement can contain any valid JavaScript code, including more nested if statements. If you want to execute more than one statement, use a block statement with curly braces ({ ... }) to group these statements together as one execution block. If you don't want to execute any statements, use an empty statement.

statement2

statement2 is a block of code that is executed if condition evaluates to false. The statement can contain any valid JavaScript code, including more nested if statements. If you want to execute more than one statement, use a block statement with curly braces ({ ... }) to group these statements together as one execution block. If you don't want to execute any statements, use an empty statement.

Flowchart of an if…else statement: The condition is an expression that evaluates to either true or false; e.g. number <= 25

condition If the value of number is less than or equal to 25, the condition evaluates to true and this block of code will execute.

If the value of number is greater than 25, the condition evaluates to false and this block of code will execute.

if code else code

after if code This is the code which comes after the if…else statement. This where execution resumes after one of the two blocks of code is executed.

IF…ELSE statement examples: example:

<script>

var name = "Jack";

if (name == "Jim") {

document.write ("Hello Jim."); }

else {

document.write ("Who are you?"); }

</script>

Result displayed in browser: Who are you?

Since the string variable name was initialized with the value "Jack", the comparison name == "Jim" will return false and the code in the if section will be skipped over and the code in the else section will be executed. If the variable



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.